﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.UI.Controls;
using Medianamik.UI.Lock;
using Medianamik.UI.Web.MVP.Presentation;

namespace Medianamik.UI.Web.MVP.View
{
    public abstract class ModuleViewWithRepository<T> : PageModuleViewWithPresenter<ModulePresenter<T>>, IModuleViewWithRepository
        where T : IModuleView
    {
        public event EventHandler Filter;

        protected readonly BaseModalWindow TypeSelectorWindow = new BaseModalWindow { ID = "TypeSelectorWindow" };
        protected readonly BaseModalWindow DeleteNodeWindow = new BaseModalWindow { ID = "deleteNodeWindow" };
        public event EventHandler<NumericalEventArgs> PageIndexChanged;
        public event EventHandler<NumericalEventArgs> PageSizeChanged;
        public event EventHandler<IdEventArgs> LockRequestFailed;
        public event EventHandler<ActivationStatusChangedEventArgs> ActivationStatusChanged;
        public event EventHandler<OrderChangedEventArgs> OrderChanged;
        public event EventHandler NodeDeleted;

        protected virtual void OnPageIndexChanged(NumericalEventArgs e)
        {
            var handler = PageIndexChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnPageSizeChanged(NumericalEventArgs e)
        {
            var handler = PageSizeChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnActivationStatusChanged(ActivationStatusChangedEventArgs e)
        {
            var handler = ActivationStatusChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnOrderChanged(OrderChangedEventArgs e)
        {
            var handler = OrderChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnLockRequestFailed(IdEventArgs e)
        {
            var handler = LockRequestFailed;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnNodeDeleted()
        {
            var handler = NodeDeleted;
            if (handler != null)
            {
                handler(this, null);
            }
        }

        protected virtual void OnFilter()
        {
            var handler = Filter;
            if (handler != null)
            {
                handler(this, null);
            }
        }

        public virtual bool EnableDelete { get { return true; } }

        public virtual void ShowLockInfoTooltip(Guid id)
        {
            LockInfoTooltip.ObjectId = id;
            LockInfoTooltip.DataBind();
            LockInfoTooltip.Show();
        }

        public abstract LockInfoToolTip LockInfoTooltip { get; }

        public virtual bool IsMultiLingual { get { return false; } }

        #region ParentNodeId

        private Guid? _parentId;
        public virtual Guid? ParentNodeId
        {
            set { ViewState["ParentNodeId"] = value; }
            get { return _parentId ?? (_parentId = GetParentId()); }
        }
        private Guid? GetParentId()
        {
            var parentNodeIdFromViewState = ViewState["ParentNodeId"] as Guid?;
            if (parentNodeIdFromViewState.HasValue)
                return parentNodeIdFromViewState.Value;

            return Request.QueryString.GetValue<Guid?>(DefaultParamNames.ParentNodeID) ?? ( NullParentNodeId ? default(Guid?) : ModuleNodeId );
        }

        #endregion

        public virtual bool NullParentNodeId
        {
            get { return false; }
        }

        public virtual Guid ModuleNodeId
        {
            get { return (ViewState["ModuleNodeId"] as Guid?).GetValueOrDefault(); }
            set { ViewState["ModuleNodeId"] = value; }
        }

        public virtual Guid BaseTypeId
        {
            get { return (ViewState["BaseTypeId"] as Guid?).GetValueOrDefault(); }
            set { ViewState["BaseTypeId"] = value; }
        }

        public virtual string TypeSelectorUrl
        {
            get { return null; }
        }

        public virtual HyperLink AddButton { get { return null; } }

        public virtual void AddTypeSelectorWindow()
        {
            MyWindowManager.Windows.Add(TypeSelectorWindow);
            TypeSelectorWindow.OnClientClose = "onNodeTypeSelected";
            TypeSelectorWindow.IconUrl = "/Medianamik/images/Icones/Ico_AjoutPage.gif";
            TypeSelectorWindow.ReloadOnShow = true;
            TypeSelectorWindow.Width = Unit.Pixel(400);
            TypeSelectorWindow.Height = Unit.Pixel(200);
            AddButton.Attributes.Add("onClick",
               "GetRadWindowManager().Open(null, 'TypeSelectorWindow');");
            AddButton.Target = "TypeSelectorWindow";
        }

        public virtual void AddDeleteNodeWindow()
        {
            MyWindowManager.Windows.Add(DeleteNodeWindow);
            DeleteNodeWindow.OnClientClose = "deleteNodeWindowCallback";
            DeleteNodeWindow.IconUrl = "/Medianamik/images/Icones/Ico_Supprimer.gif";
            DeleteNodeWindow.ReloadOnShow = true;
            DeleteNodeWindow.Width = Unit.Pixel(400);
            DeleteNodeWindow.Height = Unit.Pixel(200);
        }

        public virtual int PageIndex
        {
            get { return (int)(ViewState["PageIndex"] ?? 0); }
            set { ViewState["PageIndex"] = value; }
        }

        public virtual int PageSize
        {
            get { return (int)(ViewState["PageSize"] ?? DefaultPageSize); }
            set { ViewState["PageSize"] = value; }
        }

        public virtual ModuleViewFilters Filters
        {
            get { return ModuleViewFilters.All; }
        }

        public abstract string ListingPageUrl
        {
            get;
        }

        public virtual string RootListingPageUrl
        {
            get { return ListingPageUrl; }
        }

        public virtual string EditPageUrl
        {
            get
            {
                return Pages.ContentManagementEdition;
            }
        }

        public virtual string ChildrenListingPageUrl
        {
            get
            {
                return null;
            }
        }

        public virtual string EditReturnUrl
        {
            get { return (String)(ViewState["EditReturnUrl"] ?? ""); }
            set { ViewState["EditReturnUrl"] = value; }
        }

        public virtual string ReturnUrl
        {
            get { return (String)(ViewState["ReturnUrl"] ?? ""); }
            set { ViewState["ReturnUrl"] = value; }
        }

        public virtual int TotalCount
        {
            get { return (int)(ViewState["TotalCount"] ?? 0); }
            set { ViewState["TotalCount"] = value; }
        }

        protected virtual int DefaultPageSize { get { return 10; } }

        public virtual bool AllowPaging
        {
            get { return (bool)(ViewState["AllowPaging"] ?? false); }
            set { ViewState["AllowPaging"] = value; }
        }

        public virtual bool AllowSorting
        {
            get { return (bool)(ViewState["AllowSorting"] ?? false); }
            set { ViewState["AllowSorting"] = value; }
        }

        public virtual IEnumerable<object> DataSource
        {
            set { throw new NotImplementedException(); }
        }

        public virtual DateTime? SelectedStartDateTime
        {
            get { return null; }
            set { }
        }

        public virtual DateTime? SelectedEndDateTime
        {
            get { return null; }
            set { }
        }

        public virtual IEnumerable<ListItem> Status
        {
            get { return null; }
            set { }
        }

        public virtual string Keywords
        {
            get { return ""; }
            set { }
        }
    }
}